﻿$(function(){
	
	mywidth = $(document.body).width() / 10;
	$('#DO_MONITOR_TIME_temp').datetimebox({
		value : '4/8/2013 10:00',
		required : true,
		showSeconds : false
	});
	
	dgReload();
	
	$("#startBtn").click(function() {
		$.post('./start', function(result) {
					if (result.retrunFlag == "starting") {
						$.messager.show({
									title : '启动监控',
									msg : "启动成功！！！"
								});
					} else if (result.retrunFlag == "started") {
						$.messager.show({
									title : '启动监控',
									msg : "已经启动！！！"
								});
					}
				}, 'json');
	});
	
	$("#stopBtn").click(function() {
		$.post('./stop', function(result) {
					if (result.retrunFlag == "stopping") {
						$.messager.show({
									title : '停止监控',
									msg : "停止成功！！！"
								});
					} else if (result.retrunFlag == "stopped") {
						$.messager.show({
									title : '停止监控',
									msg : "已经停止！！！"
								});
					}
				}, 'json');
	});
	

	/**
	 * @李兆军
	 * @returns {}
	 */
	$("#editMail").click(function() {
		$('#dlg-EMAIL').dialog('open').dialog('setTitle', '修改反馈信息接收邮箱');
	});

	/**
	 * @李兆军
	 * @returns {}
	 */
	$("#SaveEmailButtons").click(function() {
		if(!$('#emailForm').form('validate')) {
			return;
		}
		$('#dlg-EMAIL').dialog('close');
		$.post('postEmail',{email:$("input[name='EMAIL']").val(),id:'1'},function(data) {
			if (data.returnFlag == 'success') {
				$.messager.show({
					title : '修改反馈信息接收邮箱',
					msg : "邮箱已更改！",
					timeout : "3000"
				});
			}
		}) ;
	});


}
);

var url;
var oldpwd;
var pwd_is_changed;
var mywidth;

function dgReload() {
	$("#dg").datagrid({
		url:'getAllServ',
		toolbar:"#toolbar",
		rownumbers:"true",
		fitColumns:"true",
		singleSelect:"true",
		columns:[[  
		          {field:'SERVER_ID',title:'编号',width:mywidth},  
		          {field:'SERVER_NAME',title:'服务器名',width:mywidth},  
		          {field:'SERVER_URL',title:'服务器URL',width:mywidth},  
		          {field:'DB_USERNAME',title:'用户名',hidden:true}, 
		          {field:'DB_DATABASE_NAME',title:'数据库名',width:mywidth},
		          {field:'DB_TABLE_NAME',title:'表名',width:mywidth},
		          {field:'DB_COLUMN_NAME',title:'字段名',width:mywidth},
		          {field:'DATA_IN_TIME',title:'标准时间',width:mywidth},
		          {field:'DO_MONITOR_TIME',title:'数据库用户名',hidden:true},
		      ]]
	});
}

function editServer() {
	var row = $('#dg').datagrid('getSelected');
	if (row) {
		$('#dlg').dialog('open').dialog('setTitle', '编辑服务器信息');
		$('#fm').form('load', row);
		// url = 'update_user.php?id='+row.id;
		url = './updateSerInfo';
		oldpwd=$('#DB_PASSWORD').val();
	} else {
		$.messager.show({
			title : '请先选择要编辑的服务器',
			msg : "请先选择要编辑的服务器！",
			timeout : "3000"
		});
	}
}

function newServer() {
	$('#dlg').dialog('open').dialog('setTitle', '添加服务器');
	$('#fm').form('clear');
	url = './addSerInfo';
}

        function saveServer(){
        	
        	//将  DO_MONITOR_TIME_temp 的数据处理后填充DO_MONITOR_TIME
        	var v = $("#DO_MONITOR_TIME_temp").datetimebox('getValue');
        	var arr1=new Array();//将date与time分割开，存在arr1
        	arr1=v.split(' ');
        	var arr2 = new Array();//将date用"/"分隔开，存在arr2
        	arr2 = arr1[0].split('/');
        	//如果月数小于10则十位补零（2 => 02）
        	if(parseInt(arr2[0])<10){
        		arr2[0] = "0"+arr2[0];
        	}
        	if(parseInt(arr2[1])<10){
        		arr2[1] = "0"+arr2[1];
        	}
        	v = arr2[2]+"-"+arr2[0]+"-"+arr2[1]+" "+arr1[1];
        	$('#DO_MONITOR_TIME').val(v);
        	
/*         	alert(oldpwd);
        	alert($('#DB_PASSWORD').val());
        	alert($('#DB_PASSWORD').val()!=oldpwd); */
        	console.log();
            if($('#DB_PASSWORD').val()!=oldpwd){
            	
            	pwd_is_changed = "yes";
            	
            	
            	//$.ajax({
            		   //type: "POST",
            		   //url: "./encrypt",
            		   //data:{inString:$("#DB_PASSWORD").val()},
            		   //success: function(data){
            			   //$("#DB_PASSWORD").val(data.outString);
            			   //alert($("#DB_PASSWORD").val(data.outString));
            		   //}
            		//});
            	
            } else{
            	pwd_is_changed = "no";
            	
            }
            $('#pwd_is_changed').val(pwd_is_changed);
            
//            alert(pwd_is_changed);
        	
            
        	$('#fm').form('submit',{  
                url: url,  
                onSubmit: function(){  
                    return $(this).form('validate');  
                },  
                success: function(result){  
                    var result = eval('('+result+')');  
                    if (result.errorMsg){  
                        $.messager.show({  
                            title: 'Error',  
                            msg: result.errorMsg  
                        });  
                    } else {  
                        $('#dlg').dialog('close');      // close the dialog  
                        $('#dg').datagrid('reload');    // reload the user data  
                    }  
                }  
            });  
        }  
 function destroyServer(){  
            var row = $('#dg').datagrid('getSelected');  
            if (row){  
                $.messager.confirm('Confirm','确定删除此服务器?',function(r){  
                    if (r){  
                        $.post('./deleteSerInfo',{SERVER_ID:row.SERVER_ID},function(result){  
                            if (result.returnFlag=="ok"){  
                                $('#dg').datagrid('reload');    // reload the user data  
                            } else {  
                                $.messager.show({   // show error message  
                                    title: 'Error',  
                                    msg: "删除失败"
                                });  
                            }  
                        },'json');  
                    }  
                });  
            }  else {
        		$.messager.show({
        			title : '请先选择要删除的服务器',
        			msg : "请先选择要删除的服务器！",
        			timeout : "3000"
        		});
        	}
        }  










